import sys
import os
import re
def validate():
    try:
        file = open('auth.dat', 'r')
        line = file.readline().strip()
        code = registcode()
        if line == code:
            return True
        else:
            return False
    except:
        return False

def checkcode(code):
    try:
        rcode = registcode()
        if rcode == code:
            file = open('auth.dat', 'w')
            file.write(code)
            return True
        else:
            return False
    except:
        return False

def machinecode():
    codemap = ((5,3,2,1,4,0),
               (9,11,8,6,10,7),
               (4,2,6,0,8,10),
               (1,11,5,3,9,7)
               )
    codeweight = (71,131, 191, 231, 291, 311)
    mac = getmac()
    code = ''
    for tupe in codemap:
        subcode = 0
        for index,weight in zip(tupe, codeweight):
            subcode += ord(mac[index])*weight
        
        str = '0000%d'%subcode
        str = str[-4:]
        code += (str+'-')
    code = code[:-1]
    return code

def registcode():
    return calccode(machinecode())

def calccode(machinecode):
    codemap = ((
                (1,9,15,13),
               (8,0,11,5),
               (10,7,3,4),
               (2,6,14,12),
               ),
               (
                (14,0,15,13),
               (9,4,1,8),
               (6,7,3,10),
               (2,5,11,12),
               ),
               (
                (12,9,5,13),
               (8,0,11,15),
               (14,3,7,4),
               (2,6,10,1),
               ),
               (
                (3,9,11,5),
               (0,6,15,13),
               (10,14,12,4),
               (2,8,7,1),
               )
               )
    codeweight = (71,131, 191, 231, 291, 311)
    map = '0123456789ABCDEF'
    mc = ''.join(machinecode.split('-'))
    code = ''
    for bigtupe in codemap:
        for tupe in bigtupe:
            subcode = 0
            for index,weight in zip(tupe, codeweight):
                subcode += ord(mc[index])*weight
            code += map[subcode %16]
        code += ('-')
    code = code[:-1]
    return code
def getmac():
    try:
        CmdLine = 'ipconfig /all'
        r = os.popen(CmdLine).read()
        if r:
            L = re.findall('([0-9,A-F]{2}-[0-9,A-F]{2}-[0-9,A-F]{2}-[0-9,A-F]{2}-[0-9,A-F]{2}-[0-9,A-F]{2})', r)
            for l in L:
                if len(l) != 17:
                    continue
                for w in '123456789ABCDEF':
                    if w in l:
                        return ''.join(l.split('-'))
        return None
    except:
        return None

if __name__ == '__main__':
    # test
    print validate()